<template>
  <div class="login">
    <div class="logo"> <img src="../assets/images/logo.png" alt=""></div>   
    <div class="content">
      <div class="content_left">
        <h3 style="color:#ffbc28;margin-left: 40px;">通过操作电商demo,您可以体验到:</h3>
        <ul>
          <li>
            <svg-icon icon-class="check" class="check_style"></svg-icon>自助式的用户全生命周期数据分析平台全貌
          </li>
          <li>
            <svg-icon icon-class="check" class="check_style"></svg-icon>场景化的业务数据分析概览
          </li>
          <li>
            <svg-icon icon-class="check" class="check_style"></svg-icon>多维度、多属性、多类型的分析模型
          </li>
          <li>
            <svg-icon icon-class="check" class="check_style"></svg-icon>用户标签的创建管理和360度用户画像洞察
          </li>
          <li>
            <svg-icon icon-class="check" class="check_style"></svg-icon>自动创建运营计划，体验流程画布实现精细运营
          </li>
        </ul>
        <div class="pics" style="background-image:url(../assets/images/help/imageB.png)">
          <img src="../assets/images/help/imageB.png" alt="">
          <img src="../assets/images/icon_01.png" alt="" class="icon-01">
          <img src="../assets/images/icon_01.png" alt="" class="icon-02">
          <img src="../assets/images/icon_01.png" alt="" class="icon-03">
          <img src="../assets/images/icon_01.png" alt="" class="icon-04">
        </div>
      </div>
      <div class="content_right">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
          <h3 class="title">小象数字营销云平台</h3>
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="请输入账号">
              <!-- <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" /> -->
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="请输入密码"
              @keyup.enter.native="handleLogin">
              <!-- <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" /> -->
            </el-input>
          </el-form-item>
          <el-form-item prop="code" v-if="captchaOnOff">
            <el-input v-model="loginForm.code" auto-complete="off" placeholder="验证码" style="width: 63%"
              @keyup.enter.native="handleLogin">
              <!-- <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" /> -->
            </el-input>
            <div class="login-code">
              <img :src="codeUrl" @click="getCode" class="login-code-img" />
            </div>
          </el-form-item>
          <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
          <div style="float: right;font-size: 14px;" v-if="register">
              <router-link class="link-type" :to="'./register'">立即注册</router-link>
            </div>
          <el-form-item style="width:100%;">
            <el-button :loading="loading" size="medium" type="primary" style="width:100%;height: 45px;background-image: linear-gradient(to right, #ffb400,#ffdb00);border-color:#ffdb00;-moz-box-shadow:2px 2px 10px #ffb400;
     -webkit-box-shadow:2px 2px 10px #ffb400;
     box-shadow:2px 2px 10px #ffb400;"
              @click.native.prevent="handleLogin">
              <span v-if="!loading">登 录</span>
              <span v-else>登 录 中...</span>
            </el-button>
            <!-- <div style="float: right;" v-if="register">
              <router-link class="link-type" :to="'./register'">立即注册</router-link>
            </div> -->
          </el-form-item>
        </el-form>
      </div>
    </div>
    <!-- <img src="../assets/images/help/imageB.png" alt=""> -->
    <!--  底部  -->
    <div class="el-login-footer">
      <span style="color:black">
        Copyright © 2019-2022 北京小象智慧科技有限公司
        备案号：京ICP备19043834号
        <el-link class="git" href="https://gitee.com/xiaoxiangopen" type="bank">
          <img src="../assets/images/gitee.png">
        </el-link>
      </span>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      cookiePassword: "",
      loginForm: {
        username: "admin",
        password: "admin123",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "用户名不能为空" }
        ],
        password: [
          { required: true, trigger: "blur", message: "密码不能为空" }
        ],
        code: [{ required: true, trigger: "change", message: "验证码不能为空" }]
      },
      loading: false,
      captchaOnOff: true,
      // 注册开关
      register: true,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaOnOff = res.captchaOnOff === undefined ? true : res.captchaOnOff;
        if (this.captchaOnOff) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(() => {
            });
          }).catch(() => {
            this.loading = false;
            if (this.captchaOnOff) {
              this.getCode();
            }
          });
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  position: relative;
  display: block;
  padding-left: 5%;
  padding-top: 7%;
  height: 100%;
  background-image: url("../assets/images/login_bg.png");
  background-size: cover;
  .logo{
    img{
      width: 10%;
      position: absolute;
      top: 20px;
    }
  }
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #ffbc28;
}

.login-form {
  border: 1px solid #f5f7f9;
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 45px 25px 35px 25px;
  box-shadow: 0px -2px 10px 0px rgb(224, 214, 214),
    0px -2px 10px 0px #f5f7f9,
    6px 0px 6px 0px #f5f7f9,
    0px 5px 19px 0px #f5f7f9;

  .el-input {
    height: 38px;

    input {
      height: 38px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-login-footer {
  position: relative;
  // height: 180px;
  height: 27%;
  // line-height: 23px;
  bottom: 0;
  width: 100%;
  text-align: left;
  // margin-top: 6%;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
  // padding-left: 63%;
  span{
    position: absolute;
    bottom:-10%;
    left: 28%;
    .git{
    margin-top: 1.765rem;
    img{
      height: 20px;
    }
    }
  }
}

.login-code-img {
  height: 38px;
}

.content {
  display: flex;
  justify-content: space-evenly;
}

.content_left {
  margin-left: 20px;
  width: 30%;

  ul {
    li {
      list-style: none;
      font-size: 14px;
      margin-top: 10px;
    }
  }
}

.check_style {
  color: #ffbc28;
  margin-right: 10px;
}

.pics img {
  margin-top: 50px;
  margin-left: 50px;
  width: 80%;
}

.pics {
  // position: relative;

  .icon-01 {
    width: 159px;
    top: 140px;
    left: 40%;
    transform: translateX(-680px);
    position: absolute;
  }
   .icon-02 {
    width: 230px;
    top: 105px;
    left: 45%;
    transform: translateX(-120px);
    position: absolute;
  }
   .icon-03 {
width: 190px;
    top: 556px;
    left: 43%;
    transform: translateX(-650px);
    position: absolute;
  }
   .icon-04 {
    width: 255px;
    top: 662px;
    left: 50%;
    transform: translateX(350px);
    position: absolute;
  }
}


html {
  overflow-y: scroll;
}
 
:root {
  overflow-y: auto;
  overflow-x: hidden;
}
 
:root .login {
  position: absolute;
}
 
 .login {
  width: 100vw;
  overflow: hidden;
}
</style>
